<?php

namespace App\Transformers;

use App\Models\User;
use App\Models\UserIm;
use League\Fractal\TransformerAbstract;

class ImTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['user'];

    public function transform(UserIm $user)
    {
        return [
            'id' => $user->id,
            'im' => $user->im,
            'alias' => $user->alias,
            'user_id' => empty($user->user_id) ? '' : $user->user_id,
            'value' => empty($user->value) ? '' : $user->value,
            'status' => empty($user->status) ? '' : $user->status,
            'created_at' => empty($user->created_at) ? '' : $user->created_at->toDateTimeString(),
            'updated_at' => empty($user->updated_at) ? '' : $user->updated_at->toDateTimeString(),
        ];
    }

    public function includeUser(UserIm $im)
    {
        return $this->nullOrItem($im->user, new UserTransformer());

    }

    public function nullOrItem($data, $transformer, $resourceKey = null)
    {
        if (is_null($data)) {
            return parent::null();
        } else {
            return parent::item($data, $transformer, $resourceKey);
        }
    }
}